﻿#include "util.h"
#include <QSettings>
#include <QObject>
#include <QApplication>
#include <QTranslator>

namespace util
{
    extern void saveSettingGeometry(QRect rect)
    {
        QSettings settings("GxImageGadget", "WindowSetting");
        settings.setValue("geometry", rect);
    }
    
    extern QRect getSettingGeometry()
    {
        QSettings settings("GxImageGadget", "WindowSetting");
        QRect rect = settings.value("geometry", QRect(100, 100, 600, 400)).toRect();
        return rect;
    }

    void saveSettingIndex(int value)
    {
        //保存index
        QSettings settings("GxImageGadget", "WindowSetting");
        settings.setValue("index", value);
    }
    
    extern int getSettingIndex()
    {
        QSettings settings("GxImageGadget", "WindowSetting");
        int index = settings.value("index").toInt();
        return index;
    }

    void saveSettingInterval(int value)
    {
        //保存interval
        QSettings settings("GxImageGadget", "WindowSetting");
        settings.setValue("interval", value);
    }

    int getSettingInterval()
    {
        QSettings settings("GxImageGadget", "WindowSetting");
        int interval = settings.value("interval", DEFAULT_SLIDESHOW_INTERVAL).toInt();
        if(interval <= 0)
        {
            interval = DEFAULT_SLIDESHOW_INTERVAL;
        }
        return interval;
    }
    
    extern void saveSettingLastDir(const QString& dir)
    {
        QSettings settings("GxImageGadget", "WindowSetting");
        settings.setValue("lastdir", dir);
    }

    extern QString getSettingLastDir()
    {
        QSettings settings("GxImageGadget", "WindowSetting");
        QString dir = settings.value("lastdir", ".").toString();
        return dir;
    }
    
    extern void saveSettingSetGeometry(QRect rect)
    {
        QSettings settings("GxImageGadget", "WindowSetting");
        settings.setValue("settinggeometry", rect);
    }

    extern QRect getSettingSetGeometry()
    {
        QSettings settings("GxImageGadget", "WindowSetting");
        QRect rect = settings.value("settinggeometry", QRect(100, 100, 800, 600)).toRect();
        return rect;
    }
    
    extern void setLanguage(QString strLanguage)
    {
        static QTranslator m_translator;
        if(strLanguage == ("English"))
        {
            //移除翻译，原文极为英文
            if (!m_translator.isEmpty())
            {
                qApp->removeTranslator(&m_translator);
            }
        }
        else if(strLanguage == ("Chinese"))
        {
            qApp->removeTranslator(&m_translator);
            if (m_translator.load(":/translate/zh.qm"))
            {
                qApp->installTranslator(&m_translator);
            }
        }
        else if(strLanguage == ("Japanese"))
        {
            qApp->removeTranslator(&m_translator);
            if (m_translator.load(":/translate/jp.qm"))
            {
                qApp->installTranslator(&m_translator);
            }
        }
        else
        {
            return;
        }
        QSettings settings("GxImageGadget", "WindowSetting");
        settings.setValue("language", strLanguage);
    }

    extern QString getCurrentLanguage()
    {
        QSettings settings("GxImageGadget", "WindowSetting");
        QString strLanguage = settings.value("language", QObject::tr("English")).toString();
        return strLanguage;
    }

    extern QStringList getLanguages()
    {
        return QStringList() << ("English") << ("Chinese") << ("Japanese");
    }
}





